package edu.spec.teacher.controller;

import edu.spec.sys.model.User;
import edu.spec.sys.model.dto.MvcReturn;
import edu.spec.sys.model.enumeration.SignInStatus;
import edu.spec.teacher.model.SignIn;
import edu.spec.teacher.model.UserSignIn;
import edu.spec.teacher.service.iface.IUserSignInService;
import org.madgecko.basic.kit.BasicKit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * Author       : MadFrog
 * Date         : 2018/10/9 19:02
 * Description  :
 */
@Controller
@RequestMapping("/teacher/userSignIn")
public class UserSignInController {

    @Autowired
    private IUserSignInService userSignInService;

    @ResponseBody
    @RequestMapping("/signIn")
    public Object signIn(Integer signInId, String description, Float latitude, Float longitude, HttpSession session){
        User user = (User)session.getAttribute("user");
        Integer userId = user.getId();
        UserSignIn usi = userSignInService.loadByUserAndSignIn(userId, signInId);
        usi.setSignInDate(new Date());
        usi.setLatitude(latitude);
        usi.setLongitude(longitude);
        if(BasicKit.isEmpty(description)){
            usi.setVerdict(SignInStatus.SUCCESS);
        }else {
            usi.setDescription(description);
            usi.setVerdict(SignInStatus.LEAVE);
        }
        userSignInService.update(usi);
        return new MvcReturn(true, "签到成功！");
    }

    @ResponseBody
    @RequestMapping("/listEffective")
    public ModelAndView listEffectiveSignIn(HttpSession session){
        User u = (User)session.getAttribute("user");
        List<SignIn> signInList = userSignInService.listEffectiveSignInByUser(u.getId());
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("signInList", signInList);
        modelAndView.setViewName("teacher/userSignInList");
        return modelAndView;
    }

}
